<template>
  <div class="user-Register">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="账户" prop="account">
        <el-input v-model.number="ruleForm.account"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pass">
        <el-input
          type="password"
          v-model="ruleForm.pass"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input
          type="password"
          v-model="ruleForm.checkPass"
          autocomplete="off"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >注册</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Message } from "element-ui";
import { register } from "../../../api/szpt/szpt_login";

export default {
  name: "UserRegister",
  data() {
    var checkAccount = (rule, value, callback) => {
      setTimeout(() => {
        if (!value) {
          return callback(new Error("账户不能为空！"));
        } else {
          if (value.toString().length > 6) {
            return callback(new Error("账户为小于6个字符或数字！"));
          } else {
            callback();
          }
        }
      }, 500);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (value.toString().length < 6 || value.toString().length > 10) {
          return callback(new Error("请输入6~10位数字作为密码！"));
        }
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pass: "",
        checkPass: "",
        account: "",
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        account: [{ validator: checkAccount, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          register({
            username: this.ruleForm.account.toString(),
            password: this.ruleForm.pass.toString(),
          }).then((resp) => {
            if (resp.code == 200 && resp.msg == "操作成功") {
              this.$modal.msgSuccess("注册成功，欢迎登录！");
            } else if (resp.code == 500) {
              this.$modal.msgWarning("该用户已存在，请重新填写用户名！");
            } else {
              Message.error({ message: "服务器被吃了,请稍后重试！" });
            }
          });
        } else {
          Message.error({ message: "注册失败！请稍后重试！" });
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style scoped>
.user-Register {
  position: relative;
  width: 400px;
  top: 50%;
  left: 45%;
  transform: translate(-50%, -50%);
}
</style>
